package com.personal.controller;

import com.personal.controller.result.BackCode;
import com.personal.controller.result.Result;
import com.personal.domain.Supplier;
import com.personal.service.SupplierService;
import com.personal.util.partDisplay.SupplierPart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @projectName: smartCommunity_springboot
 * @package: com.personal.controller
 * @className: Supplier
 * @author: bong~
 * @description: TODO
 * @date: 2023/8/9 9:56
 * @version: 1.0
 */


@RestController
@RequestMapping("/supplier")
public class SupplierController {


    @Autowired
    private SupplierService supplierService;

    @GetMapping("All")
    public Result selectAll(){
        List<Supplier> list=supplierService.selectAll();
        return new Result(list!=null?BackCode.GET_OK:BackCode.GET_ERR,list);
    }

    @GetMapping()
    public Result selectPartAll(){
        List<SupplierPart> list=supplierService.selectPartAll();
        return new Result(list!=null?BackCode.GET_OK:BackCode.GET_ERR,list);
    }

    @GetMapping("/search")
    public Result selectPartByKey(@RequestParam("name")String name,@RequestParam("code")String code){
        List<SupplierPart> list=supplierService.selectPartByKey(name,code);
        return new Result(list!=null?BackCode.GET_OK:BackCode.GET_ERR,list);
    }

    @PostMapping()
    public Result add(@RequestBody Supplier supplier){
        boolean flag=supplierService.add(supplier);
        return new Result(flag?BackCode.SAVE_OK:BackCode.SAVE_ERR,flag);
    }

    @GetMapping("updateData")
    public Result getUpdateData(@RequestParam("id") int id){
        Supplier supplier=supplierService.getUpdateData(id);
        return new Result(supplier!=null?BackCode.GET_OK:BackCode.GET_ERR,supplier);
    }
    @PutMapping()
    public Result update(@RequestBody Supplier supplier){
        boolean flag=supplierService.update(supplier);
        return new Result(flag?BackCode.UPDATE_OK:BackCode.UPDATE_ERR,flag);
    }

    @DeleteMapping()
    public Result delete(@RequestParam("ids") List<Integer> ids){
        boolean flag=supplierService.delete(ids);
        return new Result(flag?BackCode.DELETE_OK:BackCode.DELETE_ERR,flag);
    }

}
